package com.xianye.erp.service.order.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import com.xianye.erp.dao.base.DaoSupport;
import com.xianye.erp.util.PageData;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;

@Service("refreshOrderStatus")
@Scope("prototype")
public class RefreshOrderStatus {
	protected Logger log = Logger.getLogger(RefreshOrderStatus.class);
	private final static Executor executor = Executors.newCachedThreadPool();//启用多线程

	@Autowired
	private JdbcTemplate jdbc;
	@Resource(name = "daoSupport")
	private DaoSupport dao;
	@Autowired
	private ExpressOrderAPI expressOrderAPI;
	
	public void refresh(){
		try {
			List<PageData> list = getStore();
			for(int i=0;i<list.size();i++){
				getDBDate(list.get(i));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public List<PageData> getStore() throws Exception{
		return (List<PageData>) dao.findForList("StoreMapper.list", new PageData());
	}
	/**
	 * 同步单个店铺数据库中未完成订单状态
	 */
	public void getDBDate(PageData store){
		log.info("开始同步店铺"+store.get("ID")+"数据库中未完成订单,开始时间"+new Date().toLocaleString());
		try {
			List<PageData> list = (List<PageData>) dao.findForList("OrderMapper.unfinishOrder",store);
			List<PageData> temp = new ArrayList<PageData>();
			for(int i=0;i<list.size();i++){
				temp.add(list.get(i));
				if(temp.size() %50 ==0){
					deal(store, temp);
					temp.clear();
				}
			}
			if(temp.size()>0){
				deal(store, temp);
				temp.clear();
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("同步单个店铺数据库中未完成订单状态抛出异常信息：",e);
		}
		log.info("结束同步店铺"+store.get("ID")+"数据库中未完成订单状态,结束时间"+new Date().toLocaleString());
	}
	public void deal(final PageData store,List<PageData> list){
		try {
			final CountDownLatch latch = new CountDownLatch(list.size());
			for(int i=0;i<list.size();i++){
				final PageData oo = list.get(i);
				executor.execute(new Runnable() {
					@Override
					public void run() {
						try {
							JSONObject order = expressOrderAPI.getJSONOrderDetail(store, Long.parseLong(oo.getString("orderId")));
							if(order!=null &&!oo.getString("orderStatus").equals(order.getString("order_status"))){
								String sql1 = "update e_order set orderStatus = '"+order.getString("order_status")+"' where orderId='"+oo.getString("orderId")+"'";
								jdbc.execute(sql1);
							};
						} catch (Exception e) {
							e.printStackTrace();						
						}finally{
							latch.countDown();
						}
						
					}
				});
			}
			latch.await();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
